package com.micro.turtle.runner;

import cn.hutool.core.collection.CollectionUtil;
import com.micro.turtle.anon.CmdRunner;
import com.micro.turtle.constant.Cmd;
import com.micro.turtle.constant.Url;
import com.micro.turtle.constant.Url.GroupUri;
import com.micro.turtle.model.Group;
import com.micro.turtle.util.Api;
import com.micro.turtle.util.Log;
import java.util.List;

/**
 * GroupRunner
 *
 * @author jason.lv
 * @since 2022/1/20 16:23
 */
public class GroupRunner {

    public static void print(List<Group> groups) {
        if (CollectionUtil.isEmpty(groups)) {
            Log.info("Groups >>>>> has nothing");
            return;
        }
        for (Group group : groups) {
            Log.info("Group >>>>> Id:%s ||| %s ||| %s", group.getId(), group.getName(), group.getDescription());
        }
    }

    public static List<Group> list() {
        Url url = Url.create(GroupUri.LIST);
        return Api.getList(url, Group.class);
    }

    public static List<Group> subList(Integer groupId) {
        Url url = Url.create(GroupUri.SUB_LIST).group(groupId);
        return Api.getList(url, Group.class);
    }

    @CmdRunner
    public static class ListRunner extends Runner {

        public ListRunner() {
            super(Cmd.GROUP);
        }

        @Override
        void exec() {
            print(list());
        }
    }

    @CmdRunner
    public static class SubListRunner extends Runner {

        public SubListRunner() {
            super(Cmd.SUB_GROUP);
        }

        @Override
        void exec() {
            String groupId = scannerNext("输入项目组ID:");
            print(subList(Integer.valueOf(groupId)));
        }
    }
}
